package main;

import bean.AirInfo;
import dao.AirInfoDao;
import dao.impl.AirInfoDaoImpl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

public class View {

    public void welcome(){
        System.out.println("*************欢迎使用航班信息管理系统*****************");
    }

    public int  menu(){
        System.out.println("请选择操作");
        System.out.println("1.列出所有航班");
        System.out.println("2.按起飞时间查询");
        System.out.println("3.按目的地查询");
        System.out.println("4.删除航班");
        System.out.println("5.更新航班");
        System.out.println("6.离开系统");
        Scanner input=new Scanner(System.in);
        String choose=input.nextLine();
        int i = 0;
        try {
            i = Integer.valueOf(choose).intValue();
        } catch (NumberFormatException e) {
            System.out.println("输入的不是数字请重新输入");
            return menu();
        }
        if (i > 6 || i < 0) {
            System.out.println("输入的范围错误请重新输入");
            return menu();
        }
        return i;
    }

    public void  bye(){
        System.out.println("再见,欢迎下次使用");
    }

    public void getAll() {
        AirInfoDaoImpl dao = new AirInfoDaoImpl();
        List<AirInfo> airInfos = dao.getAll();
        System.out.println("\t编号"+"\t"+"\t航班号"+"\t"+"\t目的地"+"\t"+"\t起飞时间");
        for (AirInfo ai : airInfos) {
            System.out.println("\t"+ai.getId()+"\t"+"\t"+ai.getAirNo()+"\t"+"\t"+ai.getDestination()+"\t"+"\t"+ai.getDate());
        }
    }

    public void getByDestination() {
        AirInfoDaoImpl dao = new AirInfoDaoImpl();
        System.out.println("请输入航班目的地");
        Scanner input=new Scanner(System.in);
        String des=input.nextLine();
        List<AirInfo> airInfos = dao.getByDestination(des);
        System.out.println("\t编号"+"\t"+"\t航班号"+"\t"+"\t目的地"+"\t"+"\t起飞时间");
        for (AirInfo ai : airInfos) {
            System.out.println("\t"+ai.getId()+"\t"+"\t"+ai.getAirNo()+"\t"+"\t"+ai.getDestination()+"\t"+"\t"+ai.getDate());
        }
    }

    public void getByDate() throws ParseException {
        AirInfoDaoImpl dao = new AirInfoDaoImpl();
        System.out.println("请输入航班起飞日期");
        Scanner input=new Scanner(System.in);
        String d=input.nextLine();
        Date date = new SimpleDateFormat("yyyy-MM-dd").parse(d);
        List<AirInfo> airInfos = dao.getByDate(date);
        System.out.println("\t编号"+"\t"+"\t航班号"+"\t"+"\t目的地"+"\t"+"\t起飞时间");
        for (AirInfo ai : airInfos) {
            System.out.println("\t"+ai.getId()+"\t"+"\t"+ai.getAirNo()+"\t"+"\t"+ai.getDestination()+"\t"+"\t"+ai.getDate());
        }
    }

    public void delete(){
        AirInfoDaoImpl dao = new AirInfoDaoImpl();
        System.out.println("请输入要删除的航班id");
        Scanner input=new Scanner(System.in);
        String choose=input.nextLine();
        int i = 0;
        try {
            i = Integer.valueOf(choose).intValue();
        } catch (NumberFormatException e) {
            System.out.println("输入的不是数字请重新输入");
            delete();
        }
        boolean b = dao.deleteAir(i);
        if (b) {
            System.out.println("删除成功!");
        } else {
            System.out.println("删除失败!");
        }
    }

    public void update() throws ParseException {
        AirInfoDaoImpl dao = new AirInfoDaoImpl();
        System.out.println("请输入要修改的航班编号");
        Scanner input=new Scanner(System.in);
        String choose=input.nextLine();
        int id = 0;
        try {
            id = Integer.valueOf(choose).intValue();
        } catch (NumberFormatException e) {
            System.out.println("输入的不是数字请重新输入");
            update();
        }
        System.out.println("请输入新的航班编号");
        String code=input.nextLine();
        System.out.println("请输入新的航班目的地");
        String des=input.nextLine();
        System.out.println("请输入新的航班日期");
        String d=input.nextLine();
        Date date = new SimpleDateFormat("yyyy-MM-dd").parse(d);
        AirInfo airInfo = new AirInfo();
        airInfo.setId(id);
        airInfo.setAirNo(code);
        airInfo.setDestination(des);
        airInfo.setDate(date);
        boolean b = dao.updateAir(airInfo);
        if (b) {
            System.out.println("更新成功!");
        } else {
            System.out.println("更新失败!");
        }

    }


}
